﻿using System;
using System.Collections.Generic;

namespace Caffeine.Core
{
    internal class CollectionValidationMethodWrapper<T>
        where T : ModelBase, new()
    {
        private readonly CollectionValidationMethod<T> _originalValidationMethod;
        private readonly CollectionValidationMethod<T> _newValidationMethod;

        public CollectionValidationMethodWrapper(CollectionValidationMethod<T> originalValidationMethod, CollectionValidationMethod<T> newValidationMethod)
        {
            if (originalValidationMethod == null)
            {
                throw new ArgumentNullException("originalValidationMethod");
            }

            if (newValidationMethod == null)
            {
                throw new ArgumentNullException("newValidationMethod");
            }

            _originalValidationMethod = originalValidationMethod;
            _newValidationMethod = newValidationMethod;
        }

        public void ValidationMethod
        (
            ModelCollection<T> collection,
            ref IEnumerable<ValidationError> validationErrors,
            ref bool isValid
        )
        {
            OriginalValidationMethod(collection, ref validationErrors, ref isValid);

            if (isValid)
            {
                NewValidationMethod(collection, ref validationErrors, ref isValid);
            }
        }

        private CollectionValidationMethod<T> OriginalValidationMethod
        {
            get
            {
                return _originalValidationMethod;
            }
        }

        private CollectionValidationMethod<T> NewValidationMethod
        {
            get
            {
                return _newValidationMethod;
            }
        }
    }
}
